package com.cheng.reggie.service.impl;

import com.cheng.reggie.dto.OrderDto;
import com.cheng.reggie.entity.*;
import com.cheng.reggie.mapper.*;
import com.cheng.reggie.service.OrderService;
import com.cheng.reggie.util.UUIDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 查询指定用户分页数据
     *
     * @param page
     * @param pageSize
     * @param userid
     * @return
     */
    @Override
    public Page<OrderDto> findByPage(Integer page, Integer pageSize, Long userid) {
        PageHelper.startPage(page,pageSize);
        List<Orders> ordersList = orderMapper.findByUid(userid);
        PageInfo<Orders> PageInfo = new PageInfo<>(ordersList);

        //将ordersList转封装PageInfo<Orders>对象
        List<OrderDto> orderDtoList = ordersList.stream().map((order -> {
            OrderDto orderDto = new OrderDto();
            //将order里面的对象属性赋值给orderDto对象
            BeanUtils.copyProperties(order,orderDto);
            //获取当前订单的订单详细列表
            List<OrderDetail> orderDetails =  orderDetailMapper.findByOid(order.getId());
            //封装给orderDto
            orderDto.setOrderDetails(orderDetails);
            return orderDto;
        })).collect(Collectors.toList());
        //封装自己的分页数据
        Page<OrderDto> pageObj = new Page<>();
        pageObj.setTotal(PageInfo.getTotal());
        pageObj.setRecords(orderDtoList);
        pageObj.setPageSize(PageInfo.getPageSize());

        return pageObj;
    }

    /**
     * 新增订单的业务方法
     *
     * @param orders
     * @param userId
     */
    @Transactional
    @Override
    public void addOrder(Orders orders, Long userId) {
        //1.根据传递过来的默认地址id获取地址对象AddressBooks
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        //2.根据传递过来的用户的登录id获取购物车数据List<ShoopingCar>
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAllByUserId(userId);
        //3.根据登录id用户对象(得到用户名写入订单中)
        User user = userMapper.findById(userId);
        //3.1 完善订单对象Order
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        orders.setId(oid);
        orders.setNumber(oid +"");
        orders.setStatus(1); //1.代付款
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now()); //下单时间
        orders.setCheckoutTime(LocalDateTime.now()); //结算时间

        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName()); //登录用户别名
        orders.setConsignee(addressBook.getConsignee()); //收件人
        //4.完善订单明细对象List<OrderDetail>数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        //定义总金额
        BigDecimal total = new BigDecimal("0");
        //遍历购物车集合数据,封装到OrderDetail对象中去
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //将每一个购物车商品数据封装shoppingCart封装到orderDetailId
            OrderDetail orderDetail = new OrderDetail();
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail.setId(orderDetailId);
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            //将封装好的orderDetail对象加入到orderDetailList
            orderDetailList.add(orderDetail);
            //累加商品的金额 = 数量+加上单价
            total = total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()+ "")));
        }
        //完善订单主表总价
        orders.setAmount(total);

        //5.调用mapper新增订单主表数据
        orderMapper.sava(orders);
        //6.调用mapper新增能插入多条明细表数据
        orderDetailMapper.saveBatch(orderDetailList);


        //7.下单成功,清空购物车
        shoppingCartMapper.deleteByUserId(userId);
    }


}
